import requests
import csv
from lxml import etree

url = 'https://h5.izuiyou.com/hot'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'
}
r = requests.get(url=url, headers=headers)
r.encoding = 'utf-8'
csv_headers = ['用户名', '发表时间', '评论']
g = open('./最右.csv', 'w', encoding='utf-8')
g_csv = csv.writer(g)
g_csv.writerow(csv_headers)
html = etree.HTML(r.content.decode('utf-8'))
comment_time = html.xpath('//*[@id="app"]/div/div[1]/div[1]/div[1]/div/div[1]/div/div[2]/text()')
comment_name = html.xpath('//*[@id="app"]/div/div[1]/div[1]/div[1]/div/div[1]/div/div[1]/text()')
comment_text = html.xpath('//*[@id="app"]/div/div[1]/div[1]/div[1]/div/div[2]/div[1]/text()')
comment_name = [x.strip() for x in comment_name]
for i in range(int(len(comment_name))):
    info = [comment_name[i], comment_time[i], comment_text[i]]
    g_csv.writerow(info)
